<?php

namespace Home\Controller;

use Think\Controller;

/**
 * 外包客服外呼中心
 */
class OutboundManergerController extends CommnController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 调查用户列表
     */
    public function FK1_UserList()
    {
        $url_type = I('get.url_type');//1 风控未通过用户列表
        $page_first = I('get.page_first');
        $search = I('get.user');
        $this->rid = I('rid');
        $this->roles = role(I('rid'));
        $this->page = I('page');
        $this->url_type = $url_type;
        $this->user_content = $search;
        switch ($url_type) {
            case 1://风控未通过用户
                $where['c.mobile|c.truename|c.id_card'] = array('like', '%' . $search . '%');
                $where['b.vid'] = array('exp', 'is null');
                $tol = M('user_unknown', '', 'KEFU_FK')->join('a left join dxd_kefu.send_outbound_user b on a.vid= b.vid')->join('left join dxd_user.user c on a.vid=c.vid')->join('left join dxd_user.user_login d on a.vid=d.vid')->where($where)->count();
                $row = '100';
                $page = new PageController($tol, $row);
                $fpage = $page->fpage();
                if (!empty($page_first)) {
                    $page->listfirst = $this->page;
                }
                $field = 'c.vid,c.id,c.mobile,c.truename,c.cert_status,c.sex,c.ctime,d.mobilefirm';
//                $data = M('user_unknown', '', 'KEFU_FK')->query('select a.vid,a.id,a.ctime from dxd_fk.user_unknown a left join dxd_kefu.send_outbound_user b on a.vid= b.vid where b.vid is NULL ORDER BY a.ctime DESC limit ' . $page->listfirst . ',' . $page->listRows);

                $user = M('user_unknown', '', 'KEFU_FK')->join('a left join dxd_kefu.send_outbound_user b on a.vid= b.vid')->join('left join dxd_user.user c on a.vid=c.vid')->join('left join dxd_user.user_login d on a.vid=d.vid')->where($where)->field($field)->order('a.ctime desc')->limit($page->listfirst, $page->listRows)->select();

                break;
            case 2 ://注册未认证用户
                $where = [];
                $where['a.cert_status'] = array('eq', '1');
                $where['e.is_call'] = array('eq', '0');
                $where['b.vid'] = array('exp', 'IS NULL');
                $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime,d.mobilefirm,e.call_level as call_user_level';
//                $w = 'a.cert_status=1  and a.taobao_verify=1 and a.shebao_verify=1 and a.credit_verify=1 and a.jd_verify=1 and a.mobile_verify=1 and a.work_cert=1 and a.zhima_verify=0 and a.contact_verify=0 and a.ctime < UNIX_TIMESTAMP(\'' . date('Y-m-d', time()) . '\') and a.ctime > UNIX_TIMESTAMP(\'1506787200\')';
////                $num = M('user', '', 'USER')->query('select count(*) as num from dxd_user.user a left join dxd_kefu.send_outbound_user b on a.vid= b.vid where ' . $w . ' and b.vid is NULL and concat(a.mobile,a.truename) like \'%' . $search . '%\'');
                $count = M('no_mobilefirm_no_cert', '', 'KEFU_FK')->join('e left join dxd_user.user a on e.vid=a.vid ')->join('left join dxd_kefu.send_outbound_user b on e.vid = b.vid')->join('left join dxd_user.user_login d on e.vid=d.vid')->where($where)->count();
                $tol = $count;
                $row = '100';
                $page = new PageController($tol, $row);
                $fpage = $page->fpage();
                $user = M('no_mobilefirm_no_cert', '', 'KEFU_FK')->join('e left join dxd_user.user a on e.vid=a.vid ')->join('left join dxd_kefu.send_outbound_user b on e.vid = b.vid')->join('left join dxd_user.user_login d on e.vid=d.vid')->where($where)->field($field)->limit($page->listfirst, $page->listRows)->order('e.call_level asc')->select();
                break;

            case  4://注册未认证用户 有机型
                $where = [];
                $where['a.cert_status'] = array('eq', '1');
                $where['e.is_call'] = array('eq', '0');
                $where['b.vid'] = array('exp', 'IS NULL');
                $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime,d.mobilefirm,e.call_level as call_user_level';
                $count = M('mobilefirm_no_cert', '', 'KEFU_FK')->join('e left join dxd_user.user a on e.vid=a.vid ')->join('left join dxd_kefu.send_outbound_user b on e.vid = b.vid')->where($where)->count();
                $tol = $count;
                $row = '100';
                $page = new PageController($tol, $row);
                $fpage = $page->fpage();
                $user = M('mobilefirm_no_cert', '', 'KEFU_FK')->join('e left join dxd_user.user a on e.vid=a.vid ')->join('left join dxd_kefu.send_outbound_user b on e.vid = b.vid')->join('left join dxd_user.user_login d on e.vid=d.vid')->where($where)->field($field)->limit($page->listfirst, $page->listRows)->order('e.call_level asc')->select();
                break;
            case 3://认证通过未借款
                $where['c.vid'] = array('exp', 'is null');
                $where['d.vid'] = array('exp', 'is null');
//                $where['a.mobile|a.truename'] = array('like', '%' . $search . '%');
                $tol = M('cert_no_borrow', '', 'KEFU_FK')->join('b left join dxd_user.user a on b.vid=a.vid')->join('left join dxd_kefu.send_outbound_user d on b.vid= d.vid')->join('left join dxd_caiwu.user_borrow c on b.vid= c.vid ')->where($where)->count();
                $row = '100';
                $page = new PageController($tol, $row);
                $fpage = $page->fpage();
                $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime,b.call_user_level,e.mobilefirm';
//                $user = M('user', '', 'USER')->join('a left join dxd_caiwu.user_borrow b on a.vid= b.vid ')->join('left join dxd_kefu.send_outbound_user c on a.vid= c.vid')->join('left join dxd_user.user_login d on a.vid=d.vid')->where($where)->field($field)->limit( $page->listfirst,$page->listRows)->order('a.ctime desc')->select();
                $user = M('cert_no_borrow', '', 'KEFU_FK')->join('b left join dxd_user.user a on b.vid=a.vid')->join('left join dxd_kefu.send_outbound_user d on b.vid= d.vid')->join('left join dxd_caiwu.user_borrow c on b.vid= c.vid ')->join('left join dxd_user.user_login e on b.vid=e.vid')->field($field)->limit($page->listfirst, $page->listRows)->order('b.call_user_level asc')->where($where)->select();
                break;

            case 5://反欺诈用户列表
                $row = '50';
                $tol = M('user_artificial_check', '', 'KEFU_R')->where(['status' => 1])->count();
                $page = new PageController($tol, $row);
                $fpage = $page->fpage();
                $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
                $user = M('user_artificial_check', '', 'KEFU_R')->join('b join dxd_user.user a on b.vid=a.vid')->field($field)->where(['b.status' => 1])->limit($page->listfirst, $page->listRows)->select();
//                $condition['d.vid'] = array('exp', 'is null');
//                $condition['d.status'] = array('in', array(4,11,1,10));
//                $condition['_logic'] = 'OR';
//                $where['_complex']=$condition;
//                $where['a.mobile|a.truename'] = array('like', '%' . $search . '%');
//                $where['b.status'] = array('in', array(1,3));
//                $where['_logic'] = 'and';
//                $tol = M('tmp_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('left join dxd_kefu.anti_check_user d on b.vid= d.vid')->where($where)->count();
//                $row = '50';
//                $page = new PageController($tol, $row);
//                $fpage = $page->fpage();
//                $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
//                $user = M('tmp_user', '', 'KEFU')->join('b  join dxd_user.user a on b.vid=a.vid')->join('left join dxd_kefu.anti_check_user d on b.vid= d.vid')->join('left join dxd_user.user_login e on b.vid=e.vid')->order('a.ctime asc')->field($field)->distinct('b.vid')->limit($page->listfirst, $page->listRows)->where($where)->select();
                break;
            case 6://认证通过用户列表 要显示认证通过并且认证和社保认证和工作认证的 用户
                $where['a.cert_status'] = 2;
                $where['a.quota'] = array('lt', 1000);
                $where['b.vid'] = array('exp', 'is null');
                $where['_string'] = 'shebao_verify=3 OR work_verify=1';
                $tol = M('user', '', 'USER')->join('a left join dxd_kefu.cert_check_user b on a.vid=b.vid')->where($where)->count();
                $row = '50';
                $page = new PageController($tol, $row);
                $fpage = $page->fpage();
                $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
                $user = M('user', '', 'USER')->join('a left join dxd_kefu.cert_check_user b on a.vid=b.vid')->where($where)->field($field)->limit($page->listfirst, $page->listRows)->select();
                break;
        }

        foreach ($user as $k => $v) {
            if ($v['cert_status'] == '1') {
                $user[$k]['cert_status'] = '未认证';
            } else if ($v['cert_status'] == '2') {
                $user[$k]['cert_status'] = '已通过';
            } else if ($v['cert_status'] == '3') {
                $user[$k]['cert_status'] = '未通过';
            } else if ($v['cert_status'] == '4') {
                $user[$k]['cert_status'] = '认证中';
            } else if ($v['cert_status'] == '5') {
                $user[$k]['cert_status'] = '审核中';
            }
            if ($v['sex'] == '0') {
                $user[$k]['sex'] = '未知';
            } else if ($v['sex'] == '1') {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == '2') {
                $user[$k]['sex'] = '女';
            }

            $user[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
        }

        $this->assign(array(
            'user' => $user,
            'fpage' => $fpage,
            'url_type' => $url_type,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->page = I('page');
        $this->display();
    }
    /**
     * 派发给 外呼客服
     */
    public function send_add()
    {
        $send_string = I('send_string');
        $first = I('first');
        $second = I('second');
        $type = I('url_type');
        $arr = explode(',', $send_string);
        $type1 = substr($type, 0, 1);
        //判断是给个人的 还是 主管的
        $id = $first;

        $user_type = '1';// 1 company  2 person
        if (!empty($second) && trim($second) != '' && $second != 0) {
            $id = $second;
            $user_type = '2';// 1 company  2 person
            if ($first == 2) {
                $user_type = '3';
            }
        }

        $admin_name = M('admin', '', 'KEFU')->where(['id' => $id])->getField('name');
        $exit = array();//去重
        if ($type1 == '5') {//反欺诈分配
            for ($i = 0; $i < count($arr); $i++) {
                if (!in_array($arr[$i], $exit)) {
                    $exit[] = $arr[$i];
                    $save = array(
                        'out_kf_id' => $id,
                        'research_start_time' => time(),
                        'research_admin' => $admin_name,
                        'status' => 2
                    );
                    $research_adm=M('user_artificial_check', '', 'KEFU')->where(['vid'=>$arr[$i]])->getfield('research_admin');
                    if(empty($research_adm)|| $research_adm==''){
                        $upadte[]= M('user_artificial_check', '', 'KEFU')->where(['vid'=>$arr[$i]])->save($save);
                    }
                }
            }
            if(!in_array(false,$upadte)){
                $this->ajax('1001', '派发成功');
                die;
            }else{
                $this->ajax('1002', '派发失败');
                die;
            }

//            for ($i = 0; $i < count($arr); $i++) {
//                $refuse = M('anti_check_user', '', 'KEFU')->where(['vid' => $arr[$i]])->getfield('status');
//                if ($refuse == 4 ) {
//                    $arr1 = array('status' => '8', 'out_kf_id' => $id);
//                    $update = M('anti_check_user', '', 'KEFU')->where(['vid' => $arr[$i]])->save($arr1);
//                } if($refuse==1){
//                    $arr1 = array('status' => 12, 'out_kf_id' => $id);
//                    $update = M('anti_check_user', '', 'KEFU')->where(['vid' => $arr[$i]])->save($arr1);
//                }else if (empty($refuse) && !in_array($arr[$i], $exit)) {
//                    $exit[] = $arr[$i];
//                    $add[] = array(
//                        'vid' => $arr[$i],
//                        'ctime' => time(),
//                        'out_kf_id' => $id,
//                        'type' => 1
//                    );
//                }else if($refuse==11 || $refuse==10){
//                    $arr1 = array('status' =>0, 'out_kf_id' => $id);
//                    $update = M('anti_check_user', '', 'KEFU')->where(['vid' => $arr[$i]])->save($arr1);
//                }
//            }
//            if (empty($add) && $update) {
//                $this->ajax('1001', '派发成功');
//                die;
//            }
//            if (M('anti_check_user', '', 'KEFU')->AddAll($add)) {
//                $this->ajax('1001', '派发成功');
//                die;
//            }
        } else if ($type1 == '6') {//认证用户调查
            for ($i = 0; $i < count($arr); $i++) {
                $refuse = M('cert_check_user', '', 'KEFU')->where(['vid' => $arr[$i]])->find();
                if (empty($refuse) && !in_array($arr[$i], $exit)) {
                    $exit [] = $arr[$i];
                    $add[] = array(
                        'vid' => $arr[$i],
                        'ctime' => time(),
                        'out_kf_id' => $id
                    );
                }
            }

            if (M('cert_check_user', '', 'KEFU')->AddAll($add)) {
                $this->ajax('1001', '派发成功');
                die;
            }
        } else {
            for ($i = 0; $i < count($arr); $i++) {
                $add[] = array(
                    'vid' => $arr[$i],
                    'ctime' => time(),
                    'type' => $type1,
                    'out_kf_id' => $id,
                    'user_type' => $user_type,
                );
            }
            if (M('send_outbound_user', '', 'KEFU')->AddAll($add)) {
                $this->ajax('1001', '派发成功');
                die;
            }
        }

        $this->ajax('1002', '派发失败 请重试');
        die;
    }


    /**
     * 派发给 不同的客服
     */
    public function send_view()
    {
        if (empty($_POST)) {
            $txt = I('get.txt');
            $url_type = I('get.url_type');
            $w = [];
            $w['status'] = '1';

            $kf_company = M('kf_out_company', '', 'KEFU')->field('kf_company_name,id as kf_company_id')->where($w)->select();
            $this->assign(array(
                'data' => $kf_company,
                'txt' => $txt,
                'url_type' => $url_type,
            ));
        }
        $this->display();
    }

    /**
     * 重派发
     */
    public function resend_view()
    {
        if (empty($_POST)) {
            $txt = I('get.txt');
            $rid = I('get.rid');
            $kf_id = I('get.kf_id');
            $work_cert = I('get.work_cert', 0);
            $url_type = I('get.url_type');
            $where = [];
            $where['out_kf_id'] = 0;
            $where['status'] = 1;
            if ($rid == '104') {
                $where['part_id'] = 1;
                $where['role_id'] = 2;
            } else {
                $where['part_id'] = 4;
            }
            $kf = M('admin', '', 'KEFU')->where($where)->field('name,id as kf_id')->select();
            $this->assign(array(
                'data' => $kf,
                'kf_id' => $kf_id,
                'txt' => $txt,
                'url_type' => $url_type,
            ));
        }
        $this->display();
    }

    /**
     * 重新派发
     */
    public function resend_add()
    {
        $send_string = I('send_string');
        $kf_id = I('kf_id');
        $second = I('second');
        $arr = explode(',', $send_string);
        $where = [];
        $where['out_kf_id'] = $kf_id;
        $where['vid'] = array('in', $arr);
        $upfate = M('anti_check_user', '', 'KEFU')->where($where)->setField('out_kf_id', $second);
        if ($upfate) {
            $this->ajax('1001', "派发成功");
        } else {
            $this->ajax('1002', "派发失败");
        }
    }

    /**
     * 风控未通过用户列表
     */
    public function anti_fraud_view()
    {
        $this->display();
    }

    /**
     *获取客服
     */
    public function get_second()
    {
        $id = I('id');
        if ($id == '2') {
            $kf = M('admin', '', 'KEFU')->where(['status' => '1', 'out_kf_id' => 0, 'part_id' => 4])->field('name,id as kf_id')->select();
        } else {
            $kf = M('admin', '', 'KEFU')->where(['status' => '1', 'out_kf_id' => $id])->field('name,id as kf_id')->select();
        }
        $this->ajax('1001', $kf);
    }

    /**
     *客服领取用户列表
     */
    public function kf_get_user()
    {
        $row = '50';
        $tol = M('user_artificial_check', '', 'KEFU_R')->where(['status' => 1])->count();
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
        $user = M('user_artificial_check', '', 'KEFU_R')->join('b  join dxd_user.user a on b.vid=a.vid')->field($field)->where(['b.status' => 1])->limit($page->listfirst, $page->listRows)->select();
//                $condition['d.vid'] = array('exp', 'is null');
//        $condition['d.vid'] = array('exp', 'is null');
//        $condition['d.status'] = array('in', array(4, 11, 1, 10));
//        $condition['_logic'] = 'OR';
//        $where['_complex'] = $condition;
//        $where['b.status'] = array('in', array(1, 3));
//        $where['_logic'] = 'and';
//        $tol = M('tmp_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('left join dxd_kefu.anti_check_user d on b.vid= d.vid')->where($where)->count();
//        $row = '50';
//        $page = new PageController($tol, $row);
//        $fpage = $page->fpage();
//        $field = 'a.vid,a.id,a.mobile,a.truename,a.cert_status,a.sex,a.ctime';
//        $user = M('tmp_user', '', 'KEFU')->join('b  join dxd_user.user a on b.vid=a.vid')->join('left join dxd_kefu.anti_check_user d on b.vid= d.vid')->join('left join dxd_user.user_login e on b.vid=e.vid')->order('a.ctime asc')->field($field)->distinct('b.vid')->limit($page->listfirst, $page->listRows)->where($where)->select();

        foreach ($user as $k => $v) {
            if ($v['cert_status'] == '1') {
                $user[$k]['cert_status'] = '未认证';
            } else if ($v['cert_status'] == '2') {
                $user[$k]['cert_status'] = '已通过';
            } else if ($v['cert_status'] == '3') {
                $user[$k]['cert_status'] = '未通过';
            } else if ($v['cert_status'] == '4') {
                $user[$k]['cert_status'] = '认证中';
            } else if ($v['cert_status'] == '5') {
                $user[$k]['cert_status'] = '审核中';
            }

            if ($v['sex'] == '0') {
                $user[$k]['sex'] = '未知';
            } else if ($v['sex'] == '1') {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == '2') {
                $user[$k]['sex'] = '女';
            }
            $user[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
        }

        $this->assign(array(
            'user' => $user,
            'fpage' => $fpage,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     *反欺诈调查领取用户
     */
    public function get_user()
    {
        $send_string = I('send_string');
        $arr = explode(',', $send_string);
        $exit = array();//去重
        $id = $_SESSION['id'];
        $admin_name = M('admin', '', 'KEFU')->where(['id' => $id])->getField('name');
        for ($i = 0; $i < count($arr); $i++) {
            for ($i = 0; $i < count($arr); $i++) {
                if (!in_array($arr[$i], $exit)) {
                    $exit[] = $arr[$i];
                    $save = array(
                        'out_kf_id' => $id,
                        'research_start_time' => time(),
                        'research_admin' => $admin_name,
                        'status' => 2
                    );
                    $upadte[] = M('user_artificial_check', '', 'KEFU')->where(['vid' => $arr[$i]])->save($save);
                }
            }

            if (!in_array(false, $upadte)) {
                $this->ajax('1001', '派发成功');
                die;
            } else {
                $this->ajax('1002', '派发失败');
                die;
            }
        }
    }
}